package com.base.cn.platform.os.entity.exam.paper;

import com.base.cn.platform.os.common.mybatis.BaseEntity;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.exam.question.ExamQuestion;
import com.base.cn.platform.os.entity.exam.question.QuestionScoreJson;
import com.base.cn.platform.os.entity.exam.record.AnswerJson;
import com.base.cn.platform.os.entity.exam.template.ExamQuestionTemplate;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.Data;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;

/**
 * 试卷关联的试题实体
 *
 * @author s.li
 * @date 2018/12/04
 */
@Data
public class ExamPaperQuestion extends BaseEntity {
    /**试卷ID*/
    private BigDecimal examId;
    /**试题类型模板ID*/
    private Integer templateId;
    /**试题ID*/
    private BigDecimal questionId;
    /**试题分数*/
    private Integer score;
    /**试题的分数JSON串*/
    private String scoreJson;
    /**试题答案*/
    private String questionAnswer;
    /**排序*/
    private Integer sort;

    //==================================
    private int tempSort;
    /**试卷对象*/
    private ExamPaper examPaper;
    /**关联的试题对象*/
    private ExamQuestion examQuestion;
    /**试题对应的类型模板*/
    private ExamQuestionTemplate questionTemplate;
    /**试题的分数JSON对象*/
    private QuestionScoreJson questionScoreJson;
    /**试题类型名*/
    private String templateName;
    /**答案JSON对象*/
    private AnswerJson answerJson;

    /**
     * 转换分数JSON串
     */
    public void fromScoreJson(){
        if(StringUtils.isNotEmpty(this.getScoreJson())){
            Gson gson = new GsonBuilder().create();
            String _scoreJson = null;
            try {
                _scoreJson = URLDecoder.decode(this.getScoreJson(),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            this.setQuestionScoreJson(gson.fromJson(_scoreJson,QuestionScoreJson.class));
        }
    }
}
